package org.vectrics.mail.struts;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.vectrics.SystemException;
import org.vectrics.company.Company;
import org.vectrics.location.Address;
import org.vectrics.user.Person;


public class ObjectGraph {
	private Logger log = Logger.getLogger(this.getClass());
	private Map objectGraph = new HashMap();
	
	public final Set<String> getKeys() {
		return(objectGraph.keySet());
	}
	
	public final TemplateObject getObject(String key) {
		return (TemplateObject)objectGraph.get(key);
	}

	public static PersonTemplateObject personToTemplateObject(Person person) {
		PersonTemplateObject personTemplateObject = new PersonTemplateObject();
		personTemplateObject.setFirstName(person.getFirstName());
		personTemplateObject.setMiddleName(person.getMiddleName());
		personTemplateObject.setLastName(person.getLastName());
		personTemplateObject.setFullName(person.getFullName());
		if (person.getContactInfo() != null) {
			Address homeAddress = person.getContactInfo().getHomeAddress();
			Address workAddress = person.getContactInfo().getWorkAddress();
			if (homeAddress != null) {
				personTemplateObject.setHomeStreetAddress(homeAddress.getAddress1());
				personTemplateObject.setHomeStreetAddress2(homeAddress.getAddress2());
				personTemplateObject.setHomeCity(homeAddress.getCity());
				personTemplateObject.setHomeState(homeAddress.getAddress1());
				personTemplateObject.setHomePostalCode(homeAddress.getPostalCode());
				personTemplateObject.setHomeCountry(homeAddress.getCountryCode());
			}
			if (workAddress != null) {
				personTemplateObject.setWorkStreetAddress(workAddress.getAddress1());
				personTemplateObject.setWorkStreetAddress2(workAddress.getAddress2());
				personTemplateObject.setWorkCity(workAddress.getCity());
				personTemplateObject.setWorkState(workAddress.getAddress1());
				personTemplateObject.setWorkPostalCode(workAddress.getPostalCode());
				personTemplateObject.setWorkCountry(workAddress.getCountryCode());
			}
		}
		return(personTemplateObject);
	}
	
	protected TemplateObject translateObjectToGraphObject(Object originalObject) {
		TemplateObject templateObject = null;
		if (originalObject instanceof Company) {
			Company company = (Company)originalObject;
			CompanyTemplateObject companyObject = new CompanyTemplateObject();
			companyObject.setCompanyName(company.getName());
			companyObject.setPhoneNumber(company.getPhone());
			companyObject.setReferenceCode(company.getReferenceId());
			if (company.getAddress() != null) {
				companyObject.setStreetAddress(company.getAddress().getAddress1());
				companyObject.setStreetAddress2(company.getAddress().getAddress2());
				companyObject.setCountry(company.getAddress().getCountryCode());
				companyObject.setState(company.getAddress().getState());
				companyObject.setPostalCode(company.getAddress().getPostalCode());
			}
			templateObject = companyObject;
		} else if (originalObject instanceof Person) {
			Person person = (Person)originalObject;
			templateObject = personToTemplateObject(person);
		} 
		return(templateObject);
	}

	
	public final void addObjectToGraph(Object object, String prefix) {
		TemplateObject templateObject = this.translateObjectToGraphObject(object);
		if (templateObject == null) {
			//log.addAppender();
			//templateObject = object;
			throw new SystemException("Do not know how to handle object type: " + object.getClass().getName());
		}
		
		log.info("Preferred prefix for object: " + prefix);
		String proposedName = prefix;
		Object foundObject = objectGraph.get(proposedName);
		if (foundObject == null) {
			log.info("Final Prefix for object: " + proposedName);
			objectGraph.put(proposedName, templateObject);
		} else {
			int index = 1;
			boolean added = false;
			while (!added) {
				proposedName = prefix + "-" + index;
				log.info("Checking to see if prefix used: " + proposedName);
				foundObject = objectGraph.get(proposedName);
				if (foundObject == null) {
					log.info("Final Prefix for object: " + proposedName);
					objectGraph.put(proposedName, templateObject);
					added = true;
				}
				index++;
			}
		}
	}
	
}
